home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_3 / password / source / passwd.c < prev    next >
C/C++ Source or Header  |  1993-04-26  |  11KB  |  405 lines

  1. #include <exec/memory.h>
  2. #include <exec/execbase.h>
  3. #include <dos/filehandler.h>
  4. #include <intuition/intuitionbase.h>
  5. #include <devices/hardblocks.h>
  6. #include <devices/input.h>
  7. #include <devices/conunit.h>
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11. #include <proto/intuition.h>
  12.  
  13. #include <MyStartup.h>
  14. #include <MyLib.h>
  15.  
  16. /***********************************************/
  17.  
  18. LONG DoPkt1(struct MsgPort *, LONG, LONG);
  19. #pragma libcall DOSBase DoPkt1 F0 32103
  20.  
  21. /***********************************************/
  22.  
  23. #define BLOCK_SIZE 512
  24.  
  25. #define NoBlock 0xffffffff
  26.  
  27. #define BLOCKS_NEEDED 4
  28.  
  29. #define PASSWORD_SIZE 40
  30.  
  31. /***********************************************/
  32.  
  33. char __chip Hardblock[BLOCK_SIZE];
  34.  
  35. struct RDArgs *RDArgs;
  36.  
  37. struct IntuitionBase *IntuitionBase;
  38.  
  39. struct IOStdReq IORequest;
  40. struct IOStdReq InputRequest;
  41.  
  42. struct Window *MyWindow;
  43. struct Task *MyTask;
  44.  
  45. struct
  46.    {
  47.       char *Drive;
  48.    } Arguments;
  49.  
  50. char VersionString[]="$VER: Password 1.1 (26.04.93)";
  51.  
  52. ULONG PasswordBlock;
  53. int PasswordIndex;
  54.  
  55. char PasswordBuffer[PASSWORD_SIZE];
  56. int BufferIndex;
  57. BOOL Locked;
  58.  
  59. /***********************************************/
  60.  
  61. void CloseAll(int RC)
  62.  
  63. {
  64.    if (InputRequest.io_Command==IND_ADDHANDLER)
  65.       {
  66.          InputRequest.io_Command=IND_REMHANDLER;
  67.          DoIO(&InputRequest);
  68.          CloseDevice(&InputRequest);
  69.       }
  70.    CloseDevice(&IORequest);
  71.    CloseLibrary(IntuitionBase);
  72.    FreeArgs(RDArgs);
  73.    exit(RC);
  74. }
  75.  
  76. /***********************************************/
  77.  
  78. void LoadBlock(int BufferSize, ULONG BlockNumber, ULONG BlockID)
  79.  
  80. {
  81.    int i;
  82.    ULONG SummedLongs;
  83.    LONG CheckSum;
  84.    ULONG *Temp;
  85.  
  86.    if (CheckSignal(SIGBREAKF_CTRL_C))
  87.       {
  88.          PrintFault(ERROR_BREAK,"passwd");
  89.          CloseAll(RETURN_WARN);
  90.       }
  91.    IORequest.io_Command=CMD_READ;
  92.    IORequest.io_Length=BLOCK_SIZE;
  93.    IORequest.io_Data=Hardblock;
  94.    IORequest.io_Offset=BlockNumber*BLOCK_SIZE;
  95.    if (DoIO(&IORequest))
  96.       {
  97.          printf("Error reading block %ld\n",BlockNumber);
  98.          CloseAll(RETURN_FAIL);
  99.       }
  100.    if (BlockID)
  101.       {
  102.          if (BlockID!=((struct RigidDiskBlock *)Hardblock)->rdb_ID)
  103.             {
  104.                printf("Block %ld: exspected type 0x%lx got type 0x%lx\n",BlockNumber,BlockID,((struct RigidDiskBlock *)Hardblock)->rdb_ID);
  105.                CloseAll(RETURN_FAIL);
  106.             }
  107.          SummedLongs=((struct RigidDiskBlock *)Hardblock)->rdb_SummedLongs;
  108.          CheckSum=0;
  109.          Temp=(ULONG *)Hardblock;
  110.          for (i=0; i<SummedLongs; i++)
  111.             {
  112.                CheckSum=CheckSum+(*Temp++);
  113.             }
  114.          if (CheckSum)
  115.             {
  116.                printf("Checksum of block %ld is invalid.\n",BlockNumber);
  117.                CloseAll(RETURN_FAIL);
  118.             }
  119.       }
  120. }
  121.  
  122. /***********************************************/
  123.  
  124. void SearchPassword(void)
  125.  
  126. {
  127.    ULONG RDBBlock;
  128.    BOOL FoundRDB;
  129.  
  130.    FoundRDB=FALSE;
  131.    for (RDBBlock=0; RDBBlock<RDB_LOCATION_LIMIT && !FoundRDB; )
  132.       {
  133.          LoadBlock(sizeof(struct RigidDiskBlock),RDBBlock,0);
  134.          if (*(ULONG *)Hardblock==IDNAME_RIGIDDISK)
  135.             {
  136.                FoundRDB=TRUE;
  137.             }
  138.          else
  139.             {
  140.                RDBBlock++;
  141.             }
  142.       }
  143.    if (!FoundRDB)
  144.       {
  145.          printf("Sorry, I couldn't locate the RigidDiskBlock on this unit!\n");
  146.          CloseAll(RETURN_FAIL);
  147.       }
  148.    LoadBlock(sizeof(struct RigidDiskBlock),RDBBlock,IDNAME_RIGIDDISK);
  149.    PasswordBlock=((struct RigidDiskBlock *)Hardblock)->rdb_DriveInit;
  150.    while (PasswordBlock!=NoBlock)
  151.       {
  152.          LoadBlock(sizeof(struct LoadSegBlock),PasswordBlock,IDNAME_LOADSEG);
  153.          for (PasswordIndex=20; PasswordIndex<BLOCK_SIZE; PasswordIndex++)
  154.             {
  155.                if (!__builtin_strcmp(Hardblock+PasswordIndex,VersionString))
  156.                   {
  157.                      PasswordIndex+=__builtin_strlen(VersionString)+1;
  158.                      return;
  159.                   }
  160.             }
  161.          PasswordBlock=((struct LoadSegBlock *)Hardblock)->lsb_Next;
  162.       }
  163.    printf("Sorry, there is no password on this unit!\n");
  164.    CloseAll(RETURN_WARN);
  165. }
  166.  
  167. /***********************************************/
  168.  
  169. void OpenDriveDevice(void)
  170.  
  171. {
  172.    struct DosList *DosList;
  173.    struct FileSysStartupMsg *StartupMsg;
  174.    char *Device;
  175.    ULONG Unit;
  176.    int RC;
  177.  
  178.    RC=0;
  179.    DosList=LockDosList(LDF_READ | LDF_DEVICES);
  180.    if (DosList=FindDosEntry(DosList,Arguments.Drive,LDF_DEVICES))
  181.       {
  182.          StartupMsg=(struct FileSysStartupMsg *)BADDR(DosList->dol_misc.dol_handler.dol_Startup);
  183.          IORequest.io_Message.mn_ReplyPort=&((struct Process *)SysBase->ThisTask)->pr_MsgPort;
  184.          Device=((char *)BADDR(StartupMsg->fssm_Device))+1;
  185.          Unit=StartupMsg->fssm_Unit;
  186.          if (OpenDevice(Device,Unit,&IORequest,0))
  187.             {
  188.                printf("Unable to open %s unit %lu\n",Device,Unit);
  189.                RC=RETURN_FAIL;
  190.             }
  191.          else
  192.             {
  193.                printf("Locating password on %s unit %ld\n",Device,Unit);
  194.             }
  195.       }
  196.    else
  197.       {
  198.          printf("\x22%s:\x22 not found.\n",Arguments.Drive);
  199.          RC=RETURN_FAIL;
  200.       }
  201.    UnLockDosList(LDF_READ | LDF_DEVICES);
  202.    if (RC) CloseAll(RC);
  203. }
  204.  
  205. /***********************************************/
  206.  
  207. struct InputEvent * __asm __saveds InputHandler(register __a0 struct InputEvent *InputEvent)
  208.  
  209. {
  210.    struct InputEvent *InputEvents;
  211.    ULONG IntuiLock;
  212.    struct Window *ActiveWindow;
  213.  
  214.    InputEvents=InputEvent;
  215.    while (InputEvent)
  216.       {
  217.          IntuiLock=LockIBase(0);
  218.          ActiveWindow=IntuitionBase->ActiveWindow;
  219.          UnlockIBase(IntuiLock);
  220.          if (ActiveWindow==MyWindow)
  221.             {
  222.                if (InputEvent->ie_Class==IECLASS_RAWKEY)
  223.                   {
  224.                      if (!(InputEvent->ie_Code&IECODE_UP_PREFIX))
  225.                         {
  226.                            if (InputEvent->ie_Code<0x60 && !Locked)
  227.                               {
  228.                                  switch(InputEvent->ie_Code)
  229.                                     {
  230.                                        case 0x44:
  231.                                        case 0x50: PasswordBuffer[BufferIndex]=0xfe;
  232.                                                   Locked=TRUE;
  233.                                                   Signal(MyTask,SIGBREAKF_CTRL_E);
  234.                                                   break;
  235.                                        case 0x41: if (BufferIndex)
  236.                                                      {
  237.                                                         BufferIndex--;
  238.                                                      }
  239.                                                   break;
  240.                                        default:   if (BufferIndex<PASSWORD_SIZE-2)
  241.                                                      {
  242.                                                         if (InputEvent->ie_Qualifier & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  243.                                                            {
  244.                                                               InputEvent->ie_Code|=0x80;
  245.                                                            }
  246.                                                         PasswordBuffer[BufferIndex++]=InputEvent->ie_Code;
  247.                                                      }
  248.                                                   break;
  249.                                     }
  250.                               }
  251.                         }
  252.                      InputEvent->ie_Class=IECLASS_NULL;
  253.                   }
  254.             }
  255.          InputEvent=InputEvent->ie_NextEvent;
  256.       }
  257.    return(InputEvents);
  258. }
  259.  
  260. /***********************************************/
  261.  
  262. void InstallInputHandler(void)
  263.  
  264. {
  265.    static struct Interrupt Interrupt=
  266.       {
  267.          NULL,NULL,
  268.          NT_INTERRUPT,
  269.          127,
  270.          "passwd",
  271.          NULL,
  272.          (void (*)())InputHandler
  273.       };
  274.  
  275.    struct InfoData __aligned InfoData;
  276.  
  277.    MyTask=SysBase->ThisTask;
  278.    Locked=TRUE;
  279.    InputRequest.io_Message.mn_ReplyPort=IORequest.io_Message.mn_ReplyPort;
  280.    DoPkt1(((struct FileHandle *)BADDR(Input()))->fh_Type,ACTION_DISK_INFO,MKBADDR(&InfoData));
  281.    MyWindow=((struct ConUnit *)(((struct IOStdReq *)(InfoData.id_InUse))->io_Unit))->cu_Window;
  282.    if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0)) ||
  283.        OpenDevice("input.device",0,&InputRequest,0))
  284.       {
  285.          CloseAll(RETURN_FAIL);
  286.       }
  287.    InputRequest.io_Command=IND_ADDHANDLER;
  288.    InputRequest.io_Data=&Interrupt;
  289.    DoIO(&InputRequest);
  290. }
  291.  
  292. /***********************************************/
  293.  
  294. void EnterOldPassword(void)
  295.  
  296. {
  297.    int Try;
  298.  
  299.    for (Try=0; Try<3; Try++)
  300.       {
  301.          printf("Please enter the current password: ");
  302.          Flush(Output());
  303.          BufferIndex=0;
  304.          Locked=FALSE;
  305.          Wait(SIGBREAKF_CTRL_E);
  306.          printf("\n");
  307.          while (BufferIndex>=0)
  308.             {
  309.                if (PasswordBuffer[BufferIndex]!=Hardblock[PasswordIndex+BufferIndex])
  310.                   {
  311.                      printf("Wrong password!\n");
  312.                      break;
  313.                   }
  314.                BufferIndex--;
  315.             }
  316.          if (BufferIndex<0)
  317.             {
  318.                return;
  319.             }
  320.       }
  321.    CloseAll(RETURN_ERROR);
  322. }
  323.  
  324. /***********************************************/
  325.  
  326. void EnterNewPassword(void)
  327.  
  328. {
  329.    printf("Please enter the new password: ");
  330.    Flush(Output());
  331.    BufferIndex=0;
  332.    Locked=FALSE;
  333.    Wait(SIGBREAKF_CTRL_E);
  334.    printf("\n");
  335.    while (BufferIndex>=0)
  336.       {
  337.          Hardblock[PasswordIndex+BufferIndex]=PasswordBuffer[BufferIndex];
  338.          BufferIndex--;
  339.       }
  340.    printf("Enter the new password again: ");
  341.    Flush(Output());
  342.    BufferIndex=0;
  343.    Locked=FALSE;
  344.    Wait(SIGBREAKF_CTRL_E);
  345.    printf("\n");
  346.    while (BufferIndex>=0)
  347.       {
  348.          if (Hardblock[PasswordIndex+BufferIndex]!=PasswordBuffer[BufferIndex])
  349.             {
  350.                printf("Passwords do not match! Password not changed.\n");
  351.                CloseAll(RETURN_WARN);
  352.             }
  353.          BufferIndex--;
  354.       }
  355. }
  356.  
  357. /***********************************************/
  358.  
  359. void SavePassword(void)
  360.  
  361. {
  362.    int i;
  363.    long CheckSum;
  364.  
  365.    CheckSum=0;
  366.    ((struct LoadSegBlock *)Hardblock)->lsb_ChkSum=0;
  367.    for (i=0; i<((struct LoadSegBlock *)Hardblock)->lsb_SummedLongs; i++)
  368.       {
  369.          CheckSum+=((long *)Hardblock)[i];
  370.       }
  371.    ((struct LoadSegBlock *)Hardblock)->lsb_ChkSum=0-CheckSum;
  372.    printf("Saving new password to disk.\n");
  373.    IORequest.io_Command=CMD_WRITE;
  374.    IORequest.io_Length=BLOCK_SIZE;
  375.    IORequest.io_Data=Hardblock;
  376.    IORequest.io_Offset=PasswordBlock*BLOCK_SIZE;
  377.    if (DoIO(&IORequest))
  378.       {
  379.          printf("Error writing block %ld\n",PasswordBlock);
  380.          CloseAll(RETURN_FAIL);
  381.       }
  382. }
  383.  
  384. /***********************************************/
  385.  
  386. void main(void)
  387.  
  388. {
  389.    if (CommandLineLength)
  390.       {
  391.          if (RDArgs=ReadArgs("DRIVE/A",(long *)&Arguments,NULL))
  392.             {
  393.                OpenDriveDevice();
  394.                SearchPassword();
  395.                InstallInputHandler();
  396.                EnterOldPassword();
  397.                EnterNewPassword();
  398.                SavePassword();
  399.                CloseAll(RETURN_OK);
  400.             }
  401.          PrintFault(IoErr(),"passwd");
  402.       }
  403.    CloseAll(RETURN_FAIL);
  404. }
  405.